/**
 *
 */
package jMiiEditor.gui.preferences;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.MaskFormatter;
import cx.ath.choisnet.lang.introspection.IntrospectionException;
import cx.ath.choisnet.lang.introspection.IntrospectionInvokeException;
import cx.ath.choisnet.lang.introspection.method.DefaultIntrospectionItem;
import cx.ath.choisnet.swing.introspection.SwingIntrospector;
import cx.ath.choisnet.swing.introspection.SwingIntrospectorException;
import cx.ath.choisnet.swing.introspection.SwingIntrospectorIllegalAccessException;

//VS 4E -- DO NOT REMOVE THIS LINE!
public class JMiiEditorPreferenceJDialog extends JDialog {

    private static final long  serialVersionUID  = 1L;
    private SwingIntrospector<JMiiEditorPreferenceJDialog,Preferences,DefaultIntrospectionItem<Preferences>> introspector;
    private JLabel jLabel_SystemID;
    private JFormattedTextField jFormattedTextField_FMTSystemId$root;
    private JPanel jPanelPrefs;
    private JLabel jLabelMiiCreator;
    private JTextField jTextField_MiiCreator$root;
//    private LimitedIntegerJTextField limitedIntegerJTextField_SystemId$root$0;
//    private LimitedIntegerJTextField limitedIntegerJTextField_SystemId$root$1;
//    private LimitedIntegerJTextField limitedIntegerJTextField_SystemId$root$2;
//    private LimitedIntegerJTextField limitedIntegerJTextField_SystemId$root$3;
    private JButton jButton_OK;
    private JButton jButton_Cancel;
    private JPanel jPanel_Bottom;
    private Preferences preferences = new Preferences();
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
    public JMiiEditorPreferenceJDialog()
    {
        initComponents();
        initFixComponents();
    }

    public JMiiEditorPreferenceJDialog( Frame parent )
    {
        super( parent );
        initComponents();
        initFixComponents();
    }

    public JMiiEditorPreferenceJDialog(
            Frame parent,
            String title,
            boolean modal
            )
    {
        super( parent, title, modal );

        initComponents();
        initFixComponents();
    }

    private void initComponents() {
        //setFont(new Font("Dialog", Font.PLAIN, 12));
        //setBackground(Color.white);
        //setForeground(Color.black);
        add(getJPanel_Prefs(), BorderLayout.CENTER);
        add(getJPanel_Bottom(), BorderLayout.SOUTH);
        //setSize(180, 100);
        super.setMinimumSize( new Dimension(180,100) );
    }

    private JLabel getJLabel_SystemID() {
        if (jLabel_SystemID == null) {
            jLabel_SystemID = new JLabel();
            jLabel_SystemID.setHorizontalAlignment(SwingConstants.RIGHT);
            jLabel_SystemID.setText("System ID");
        }
        return jLabel_SystemID;
    }

    private JLabel getJLabel_MiiCreator() {
        if (jLabelMiiCreator == null) {
            jLabelMiiCreator = new JLabel();
            jLabelMiiCreator.setHorizontalAlignment(SwingConstants.RIGHT);
            jLabelMiiCreator.setText("Mii Creator");
        }
        return jLabelMiiCreator;
    }

    private void initFixComponents()
    {
        this.introspector = SwingIntrospector.buildSwingIntrospector(
                JMiiEditorPreferenceJDialog.class,
                Preferences.class
                );

        System.out.println( introspector );

//        limitedIntegerJTextField_SystemId$root$0.setRadix( 16 );
//        limitedIntegerJTextField_SystemId$root$1.setRadix( 16 );
//        limitedIntegerJTextField_SystemId$root$2.setRadix( 16 );
//        limitedIntegerJTextField_SystemId$root$3.setRadix( 16 );

        try {
            introspector.initComponentsWithException( this );
        }
        catch( SwingIntrospectorIllegalAccessException e ) {
            e.printStackTrace();
        }
        catch( SwingIntrospectorException e ) {
            e.printStackTrace();
        }
    }

    private MaskFormatter getFMTSystemId_MaskFormatter()
    {
        MaskFormatter mask = null;

        try {
            //
            // Create a MaskFormatter for accepting hex numbers, the # symbol accept
            // only a number. We can also set the empty value with a place holder
            // character.
            //
            mask = new MaskFormatter("HH-HH-HH-HH");
            mask.setPlaceholderCharacter('_');
            }
        catch (ParseException e) {
            e.printStackTrace();
            }
        //mask.install( jFormattedTextField_FMTSystemId$root );
        return mask;
    }

    private JTextField getJTextField_MiiCreator() {
        if (jTextField_MiiCreator$root == null) {
            jTextField_MiiCreator$root = new JTextField();
            jTextField_MiiCreator$root.setColumns(10);
        }
        return jTextField_MiiCreator$root;
    }

    private JFormattedTextField getJFormattedTextField_FMTSystemId() {
        if (jFormattedTextField_FMTSystemId$root == null) {
            jFormattedTextField_FMTSystemId$root = new JFormattedTextField(getFMTSystemId_MaskFormatter());
            //jFormattedTextField_SystemId$root.setText("jFormattedTextField_0");
        }
        return jFormattedTextField_FMTSystemId$root;
    }

    private JButton getJButton_Cancel() {
        if (jButton_Cancel == null) {
            jButton_Cancel = new JButton();
            jButton_Cancel.setText("Cancel");
            jButton_Cancel.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent event) {
                    jButton_CancelMouseMousePressed(event);
                }
            });
        }
        return jButton_Cancel;
    }

    private JButton getJButton_OK() {
        if (jButton_OK == null) {
            jButton_OK = new JButton();
            jButton_OK.setText("OK");
            jButton_OK.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent event) {
                    jButton_OKMouseMousePressed(event);
                }
            });
        }
        return jButton_OK;
    }

    private JPanel getJPanel_Prefs() {
        if (jPanelPrefs == null) {
            jPanelPrefs = new JPanel();
            jPanelPrefs.setLayout(new GridLayout(2, 2));
            jPanelPrefs.add(getJLabel_MiiCreator());
            jPanelPrefs.add(getJTextField_MiiCreator());
            jPanelPrefs.add(getJLabel_SystemID());
            jPanelPrefs.add(getJFormattedTextField_FMTSystemId());
        }
        return jPanelPrefs;
    }

    private JPanel getJPanel_Bottom() {
        if (jPanel_Bottom == null) {
            jPanel_Bottom = new JPanel();
            jPanel_Bottom.add(getJButton_OK());
            jPanel_Bottom.add(getJButton_Cancel());
        }
        return jPanel_Bottom;
    }

    @SuppressWarnings("unused")
    private static void installLnF()
    {
        try {
            String lnfClassname = PREFERRED_LOOK_AND_FEEL;
            if( lnfClassname == null )
                lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel( lnfClassname );
        }
        catch( Exception e ) {
            System.err.println( "Cannot install " + PREFERRED_LOOK_AND_FEEL
                    + " on this platform:" + e.getMessage() );
        }
    }

    /**
     * Main entry of the class.
     * Note: This class is only created so that you can easily preview the result at runtime.
     * It is not expected to be managed by the designer.
     * You can modify it as you like.
     * @param args
     * @throws IOException
     */
    public static void main( String[] args )
        throws IOException
    {
        final Preferences prefs = new Preferences();

        prefs.load();

        installLnF();
        SwingUtilities.invokeLater( new Runnable() {
            @Override
            public void run()
            {
                JMiiEditorPreferenceJDialog dialog = new JMiiEditorPreferenceJDialog();
                dialog.setDefaultCloseOperation( JMiiEditorPreferenceJDialog.DISPOSE_ON_CLOSE );
                dialog.setTitle( "JMiiEditorPreferenceJDialog" );
                dialog.setLocationRelativeTo( null );
                dialog.getContentPane().setPreferredSize( dialog.getSize() );
                dialog.pack();
                dialog.setPreferences( prefs );
                dialog.setVisible( true );
//                try {
//                    dialog.getPreferences().save();
//                    System.out.println("saved");
//                }
//                catch( IOException e ) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
            }
        } );
    }

    private void jButton_CancelMouseMousePressed(MouseEvent event)
    {
        dispose();
    }

    private void jButton_OKMouseMousePressed(MouseEvent event)
    {
        try {
            this.introspector.populateObjectWithException( this, preferences );
        }
        catch( SwingIntrospectorException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch( IntrospectionException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dispose();
    }

    /**
     * @return the preferences
     */
    public Preferences getPreferences()
    {
        return preferences;
    }

    /**
     * @param preferences the preferences to set
     */
    public void setPreferences( Preferences preferences )
    {
        this.preferences = preferences;

        try {
            introspector.populateFrameWithException( this, preferences );
            }
        catch( IntrospectionInvokeException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        catch( SwingIntrospectorException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
    }


}
